package cn.pconline.autoclub.web.admin;

import cn.pconline.autoclub.entity.ClubUpgradePrize;
import cn.pconline.autoclub.service.ClubUpgradePrizeService;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.gelivable.web.Env;
import org.gelivable.web.EnvUtils;
import org.gelivable.webmvc.JSONBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin")
public class ClubUpgradePrizeController {

    @Autowired
    private ClubUpgradePrizeService clubUpgradePrizeService;
    private static final Logger log = LoggerFactory.getLogger(ClubUpgradePrizeController.class);

    private void successMsg(String msg, String navTabId, HttpServletResponse resp) {
        try {
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().println(new JSONBuilder()
                    .put("statusCode", 200)
                    .put("message", msg)
                    .put("navTabId", navTabId)
                    .toString());
        } catch (IOException ex) {
            log.error("set successMsg fail!", ex);
        }
    }

    private void errorMsg(String msg, HttpServletResponse resp) {
        try {
            resp.setCharacterEncoding("UTF-8");
            resp.getWriter().println(new JSONBuilder()
                    .put("statusCode", 300)
                    .put("message", msg)
                    .toString());
        } catch (IOException ex) {
            log.error("set errorMsg fail!", ex);
        }
    }

    /**
     * 等级奖品项目列表
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/upgradePrize.do" )
    public String listgradePrize(HttpServletRequest req, HttpServletResponse resp) {
        req.setAttribute("upgradePrize", clubUpgradePrizeService.getMap(true));
        return "admin/club/upgradePrize";
    }

    /**
     * 添加奖品项目
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/addUpgradePrize.do")
    public String addUpgradePrize(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        long levelId = env.paramLong("levelId");
        String prize = env.param("prize");

        ClubUpgradePrize upgradePrize = new ClubUpgradePrize();
        upgradePrize.setLevelId(levelId);
        upgradePrize.setPrize(prize);

        try {
            clubUpgradePrizeService.create(upgradePrize);
        } catch (DataAccessException e) {
            log.error(e.getMessage());
            this.errorMsg("添加失败，请稍后重试(" + e.getMessage() + ")", resp);
        }

        log.info("create ClubUpgradePrize where levelId = " + levelId);
        this.successMsg("添加成功", "setup-upgrade-prize", resp);

        return null;
    }
    /**
     * 修改奖品项目
     *
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "/club/editUpgradePrize.do" )
    public String editUpgradePrize(HttpServletRequest req, HttpServletResponse resp) {
        Env env = EnvUtils.getEnv();
        long levelId = env.paramLong("levelId");
        String prizes = env.param("prizes");
        String[] array = prizes.split(",");

        List<ClubUpgradePrize> list = clubUpgradePrizeService.getByLevelId(levelId);
        if (list.isEmpty()) {
            log.error("没有奖品项目...");
            this.errorMsg("数据异常!", resp);
            return null;
        }

        ClubUpgradePrize c;
        for (int i = 0, size = list.size(); i < size; i++) {
            c = list.get(i);
            c.setPrize(array[i].trim());
        }
        clubUpgradePrizeService.updateClubUpgradePrizes(list);

        this.successMsg("修改成功", "setup-upgrade-prize", resp);

        return null;
    }
}
